﻿using System;
using System.Linq;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Azad;
using NazSoft.Framework;
using System.Collections.Generic;
using BCL.Entity;
using NazSoft.Framework.Extensions;

public partial class MasterPage : System.Web.UI.MasterPage
{
    //private NameValueCollection dateInfo = null;
    DateTime? today = null;

    protected void Page_Load(object sender, EventArgs e)
    {

        if (Request.AppRelativeCurrentExecutionFilePath.ToLowerInvariant().Contains("error.aspx"))
        {
            return;
        }

        SilverlightService silverlightService = new SilverlightService();
        BindSlider();
        var logoHeadlines = silverlightService.GetHeadlines();
        var newsMarque = logoHeadlines.Detail.Where(x => x.MenuId == 12).FirstOrDefault();
        if (newsMarque != null)
        {
            NewsRepeater1.DataSource = newsMarque.DetailToDetail;
            NewsRepeater1.DataBind();
        }
        if (!IsPostBack)
        {
            form1.Action = Request.Url.ToString();
            bool preview = Convert.ToBoolean(Request.QueryString["p"]);
            today = DatetimeUtility.GetDateFromQueryString();
            var logoInfo = silverlightService.GetByCurrentId(today, preview);
            if (logoInfo != null)
            {
                today = logoInfo.CurrentDate;
            }
            else
                return;


            var navigateUrl = string.Format("/{0}/{1}/{2}/{3}", today.Value.Year, today.Value.Month.ZeroPad(), today.Value.Day.ZeroPad(), "aaj-ka-akhbar");
            //Lnklogo.HRef = navigateUrl;
            //LnkHeader.HRef = navigateUrl;
        }
        #region bind commercials
        if (today.HasValue)
        {
            var commercials = silverlightService.GetCommercial(today.Value);
            if (commercials != null)
            {
                var commercialsByLookup = silverlightService.GetCommercialMenu();
                #region Commercial Top Add
                var commercialAddtop = commercials.Where(t => t != null && t.TypeId == 101).FirstOrDefault();
                if (null != commercialAddtop)
                    pnlAddtop.InnerHtml = commercialAddtop.Script;
                else
                {
                    var v = commercialsByLookup.Where(t => t != null && t.LookUpID == 101).FirstOrDefault();
                    pnlAddtop.InnerHtml = v.ExtraData;
                }
                #endregion

                #region Commercial Bottom 1
                commercialAddtop = commercials.Where(t => t != null && t.TypeId == 102).FirstOrDefault();
                if (null != commercialAddtop)
                    pnlFrontBottom1.InnerHtml = commercialAddtop.Script;
                else
                {
                    var v = commercialsByLookup.Where(t => t != null && t.LookUpID == 102).FirstOrDefault();
                    pnlFrontBottom1.InnerHtml = v.ExtraData;
                }
                #endregion

                #region Commercial News Week1
                commercialAddtop = commercials.Where(t => t != null && t.TypeId == 103).FirstOrDefault();
                if (null != commercialAddtop)
                    pnlNewsWeek1.InnerHtml = commercialAddtop.Script;
                else
                {
                    var v = commercialsByLookup.Where(t => t != null && t.LookUpID == 103).FirstOrDefault();
                    pnlNewsWeek1.InnerHtml = v.ExtraData;
                }
                #endregion

                #region Commercial Bottom 2
                commercialAddtop = commercials.Where(t => t.TypeId == 105).FirstOrDefault();
                if (null != commercialAddtop)
                    pnlFrontBottom2.InnerHtml = commercialAddtop.Script;
                else
                {
                    var v = commercialsByLookup.Where(t => t != null && t.LookUpID == 105).FirstOrDefault();
                    pnlFrontBottom2.InnerHtml = v.ExtraData;
                }
                #endregion

                #region Commercial News Week2
                commercialAddtop = commercials.Where(t => t.TypeId == 104).FirstOrDefault();
                if (null != commercialAddtop)
                    pnlNewsWeek2.InnerHtml = commercialAddtop.Script;
                else
                {
                    var v = commercialsByLookup.Where(t => t != null && t.LookUpID == 104).FirstOrDefault();
                    pnlNewsWeek2.InnerHtml = v.ExtraData;
                }
                #endregion
            }
        }
        #endregion

    }

    private void BindSlider()
    {


        var collection = Request.QueryString;
        String mn = collection["mn"] ?? "taza-tareen"; // menu name
        string mp = collection["mp"];
        bool preview = Convert.ToBoolean(collection["preview"]);
        SilverlightService silverlightService = new SilverlightService();
        List<LookUp> lstLookUp = new List<LookUp>();

        var today = DatetimeUtility.GetDateFromQueryString(collection);

        var logoInfo = silverlightService.GetByCurrentId(today, preview);
        if (logoInfo != null)
        {
            today = logoInfo.CurrentDate;
            var details = logoInfo.Detail.Where(x => x.MenuId == 11).FirstOrDefault();
            var lstTopMenu = silverlightService.GetTopMenu();
            foreach (var item in details.DetailToDetail)
            {
                var itmes = lstTopMenu.Where(t => t.LookUpID == item.MenuId).FirstOrDefault();
                if (itmes != null)
                    lstLookUp.Add(itmes);
            }
            rptType.DataSource = lstLookUp;
            rptType.DataBind();
            lblDate.Text = today.Value.ToString("D");
            if (details != null)
            {
                if (today.HasValue)
                {
                    var navigateUrl = string.Format("/{0}/{1}/{2}/", today.Value.Year, today.Value.Month.ZeroPad(), today.Value.Day.ZeroPad());


                    lnkFirstPage.HRef = navigateUrl + "page-" + (1) + "";
                    lnkLastPage.HRef = navigateUrl + "page-" + (details.DetailToDetail.Count) + "";
                }
            }
        }
    }
    protected void lstMenu_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            var lookup = (BCL.Entity.LookUp)e.Item.DataItem;
            if (lookup != null)
            {
                var newsTypeHyperlink = (HtmlAnchor)e.Item.FindControl("NewsTypeHyperlink");
                if (newsTypeHyperlink != null)
                {
                    string menuName = lookup.Description;
                    SilverlightService silverlightService = new SilverlightService();
                    var collection = Request.QueryString;
                    var today = DatetimeUtility.GetDateFromQueryString(collection);
                    bool preview = Convert.ToBoolean(collection["preview"]);
                    var logoInfo = silverlightService.GetByCurrentId(today, preview);
                    if (logoInfo != null)
                    {
                        today = logoInfo.CurrentDate;
                    }

                    //var today = DatetimeUtility.GetDateFromQueryString();
                    if (today.HasValue)
                    {
                        var navigateUrl = string.Format("/{0}/{1}/{2}/{3}", today.Value.Year, today.Value.Month.ZeroPad(), today.Value.Day.ZeroPad(), menuName);
                        newsTypeHyperlink.HRef = navigateUrl;
                    }
                    //string.Format("/{0}/{1}/{2}/{3}", today.Value.Year, today.Value.Month.ZeroPad(), today.Value.Day.ZeroPad());
                    if (lookup.LookUpID == 11)
                    {
                        newsTypeHyperlink.Target = "_blank";
                        newsTypeHyperlink.HRef = newsTypeHyperlink.HRef + "?mp=b";
                    }
                }
            }
        }
    }

    protected void txtArchive_TextChanged(object sender, EventArgs e)
    {
        DateTime newsDate = DateTime.Today.Date;
        if (DateTime.TryParse(txtArchive.Text, out newsDate))
        {
            var navigateUrl = string.Format("/{0}/{1}/{2}/", newsDate.Year, newsDate.Month.ZeroPad(), newsDate.Day.ZeroPad());
            Response.RedirectPermanent(navigateUrl);
        }
    }
}
